package com.Drools.敲定的几种模式;

import org.kie.api.KieBase;
import org.kie.api.KieBaseConfiguration;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieRepository;
import org.kie.api.builder.model.KieBaseModel;
import org.kie.api.builder.model.KieModuleModel;
import org.kie.api.builder.model.KieSessionModel;
import org.kie.api.io.Resource;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;

import java.io.File;
import java.util.*;

public class 自定义文件路径 {
    public static List<Resource> getResources(String allFile){
        File file = new File(allFile);
        List<Resource> resources = new ArrayList<>();
        File[] list = file.listFiles();//返回file目录下的所有文件名字
        for(File s : list){
            db(resources,s);
        }

        return resources;
    }
    public static void db(List<Resource> resources,File file){
        if(file.getName().endsWith(".drl")){//看后缀为 .drl 的
            resources.add(ResourceFactory.newFileResource(file));//文件读入
        }else {
            for(File s : file.listFiles()){
                db(resources,s);
            }
        }
    }


    public static void main(String[] args) {

        List<Resource> resources;// = getResources("D:\\Work\\drools\\rules");//扫描此路径下的 所有  xx.drl 文件
        resources = new ArrayList<>();
//        resources.add(ResourceFactory.newFileResource("D:/Work/drools/rules/1/d.ruler2.drl"));
        resources.add(ResourceFactory.newFileResource("D:/Work/drools/rules/testin.drl"));

        KieServices kieServices = KieServices.Factory.get();
        KieModuleModel kieModuleModel = 自定义文件路径.getKieModuleModel_xml(kieServices);

//        kieModuleModel.
        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();

        resources.forEach(kieFileSystem::write);//批量写入 规则

        kieFileSystem.writeKModuleXML(kieModuleModel.toXML());//写入java 拼写的kmodule.xml文件

        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
        kieBuilder.buildAll();//将 kieBuilder 里的规则 构建

        System.out.println(kieModuleModel.getKieBaseModels());

        //将 新的规则 在kieContainer里重新构建
        KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());

//        KieContainer kieContainer = kieServices.getKieClasspathContainer();
//        System.out.println(kieServices.newKieClasspathContainer().getKieBaseNames());

//        if(true) return;

        System.out.println("---------getKieBaseNames---------------------------");
        System.out.println(kieContainer.getKieBaseNames());
        KieBase kieBase = kieContainer.getKieBase("myKbase0");
//        KieBase kieBase = kieContainer.getKieBase();
        KieSession kieSession = kieBase.newKieSession();

        Map<String,Object> m = new HashMap<>();
        m.put("len",5);
        Map<String,Object> m1 = new HashMap<>();
        m1.put("mm1","11");
        m1.put("li", new ArrayList());
        m1.put("ma", new HashMap<>());
//        if(m1 instanceof HashMap)
//        kieSession.insert(m);
        ArrayList<Object> arrayList = new ArrayList<>();
        arrayList.add("1");

        kieSession.setGlobal("sss",arrayList);
        kieSession.insert("/awdwsds1");
        int i = kieSession.fireAllRules();//使用规则
        System.out.println(i);
        kieSession.dispose();//关闭规则

    }

    public static List<Map<String,Object>> ss(Object o){
        if(o instanceof Map){
            List<Map<String,Object>> objects = new ArrayList<>();
            objects.add((Map)o);
            return objects;
        }else {
            return (List<Map<String,Object>>) o;
        }
    }

    //自定义 xml
    private static KieModuleModel getKieModuleModel_xml(KieServices kieServices) {
        System.out.println("---------KieModuleModel--------");
        KieModuleModel kieModuleModel = kieServices.newKieModuleModel();

        KieBaseModel kieBaseModel = kieModuleModel.newKieBaseModel("myKbase0");
        kieBaseModel.addPackage("rules");//添加 Package == 规则文件的 package xx  的xx
//        kieBaseModel.addPackage("DRules");
        kieBaseModel.setDefault(true);//指定当前否为默认

        KieSessionModel kieSessionModel = kieBaseModel.newKieSessionModel("ksession1");
        kieSessionModel.setDefault(true);

        System.out.println("------getPackages----------------------");
        List<String> packages = kieBaseModel.getPackages();//获取此Model 有哪些包

        System.out.println(kieModuleModel.toXML());//xml的展示

        return kieModuleModel;
    }
}
